package com.android.music.prolyric;

import java.util.ArrayList;
import java.util.HashMap;

import com.android.music.prolyric.songmanager.Song;
import com.android.music.prolyric.songmanager.SongsManager;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class LibraryArtist extends Activity {
	private static final int STATE_SELECT_ARTIST = 0 ;
	private static final int STATE_SELECT_SONG	= 1;
	private int state = 0;
	private ArrayList<HashMap<String, String>> songs;
	private ArrayList<HashMap<String, String>> artists;
	private ListView lvArtist;
	private SongsManager songManager;
	
	int colorArtist,colorCountSong;
	int colorSong,colorSinger,colorTime;
	private SharedPreferences prefs;
	private SimpleAdapterArtist artistAdap;
	private SimpleAdapterLib adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.artists_list);
		
		prefs = getSharedPreferences("libraryColor", MODE_PRIVATE);
		colorCountSong=(int) prefs.getInt("countsongcolor",0xffffffff);		
		colorArtist=(int) prefs.getInt("artistcolor",0xffffffff);
		
		colorSong= (int) prefs.getInt("songcolor",0xffffffff);
		colorSinger= (int) prefs.getInt("singercolor",0xffffffff);
		colorTime=(int) prefs.getInt("timecolor",0xffffffff);
		
		 songManager = new SongsManager(this);
		 lvArtist =(ListView)findViewById(R.id.lvArtists);
		 setFirstAdapter();
		 lvArtist.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(state == STATE_SELECT_ARTIST)
				{
					songs = songManager.getSongsByArtist(artists.get(arg2).get("artistname"));

					adapter = new SimpleAdapterLib(LibraryArtist.this, songs, R.layout.song_list_row, new String[]{"title","artist","duration"}, new int[]{R.id.tvsongtitle,R.id.tvsongartist,R.id.tvsongduration},colorSong,colorSinger,colorTime);
					lvArtist.setAdapter(adapter);
					state = STATE_SELECT_SONG;
				}
				else if(state == STATE_SELECT_SONG)
				{
					GlobalState globle = (GlobalState) getApplication();
					globle.setGlobalSongs(songs);
					globle.setSongIndex(arg2);
					if(getParent()==null)
						setResult(Activity.RESULT_OK);
					else 
						getParent().setResult(Activity.RESULT_OK);
					finish();
				}
				
			}
		});
	}
	
	public void setFirstAdapter()
	{
		 artists = songManager.getAllArtist();
		 artistAdap = new SimpleAdapterArtist(this, artists, R.layout.artists_list_row, new String[]{"artistname","numberOfSong"}, new int[]{R.id.tvArtistsName,R.id.tvArtistsCountSong},colorArtist,colorCountSong);
		 lvArtist.setAdapter(artistAdap);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		setFirstAdapter();
		state = STATE_SELECT_ARTIST;
	}
	  
}
